package com.ycg.vue.modules.system.service.impl;

import com.ycg.vue.Entity.OperationLogEntity;
import com.ycg.vue.Enum.LogEnum;
import com.ycg.vue.constant.Constant;
import com.ycg.vue.context.ThreadLocalContext;
import com.ycg.vue.modules.system.Dto.MaterialLibraryDto;
import com.ycg.vue.modules.system.Entity.MaterialLibraryEntity;
import com.ycg.vue.modules.system.mapper.MaterialLibraryMapper;
import com.ycg.vue.modules.system.mapper.SystemMapper;
import com.ycg.vue.modules.system.service.MaterialLibraryService;
import com.ycg.vue.result.Result;
import com.ycg.vue.utils.UUIDUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Description
 * @Author jink
 * @Date 2024/6/19
 */
@Service
public class MaterialLibraryServiceImpl implements MaterialLibraryService {

    @Resource
    private MaterialLibraryMapper materialLibraryMapper;

    @Resource
    private SystemMapper systemMapper;
    @Override
    public Result addNew(MaterialLibraryEntity entity) {
        entity.setId(UUIDUtils.getUUID());
        entity.setUploadTime(new Date());
        entity.setUploadBy(ThreadLocalContext.getUserId());
        materialLibraryMapper.insert(entity);
        return Result.success();
    }

    @Override
    public Result list(MaterialLibraryDto dto) {
        return Result.success(materialLibraryMapper.list(dto));
    }

    @Override
    public Result changeMaterialStatus(MaterialLibraryDto dto) {
        MaterialLibraryEntity entity = materialLibraryMapper.getById(dto.getId());
        if(entity == null) return Result.error(Constant.MATERIAL_STATUS_ERROR);
        materialLibraryMapper.updateStatusById(dto);
        return Result.success();
    }

    @Override
    public Result deleteMaterial(String id) {
        MaterialLibraryEntity entity = materialLibraryMapper.getById(id);
        if(entity == null) return Result.error(Constant.MATERIAL_STATUS_ERROR);
        materialLibraryMapper.deleteById(id);
        systemMapper.insertLog(OperationLogEntity.builder().type(LogEnum.DELETE.getName()).operation(id).success(true).build());
        return Result.success();
    }
}
